package edu.qichen.onlineBlog.comments.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.qichen.onlineBlog.blogs.dto.BlogUserPageDto;
import edu.qichen.onlineBlog.comments.dto.*;
import edu.qichen.onlineBlog.comments.mapper.CommentsMapper;
import edu.qichen.onlineBlog.comments.pojo.Comments;
import edu.qichen.onlineBlog.comments.serivce.CommentsService;
import edu.qichen.onlineBlog.framework.utils.TokenUtils;
import edu.qichen.onlineBlog.framework.validata.ValidatorUtil;
import edu.qichen.onlineBlog.likes.dto.LikesBlogsUserPageDto;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 功能:
 * 作者:程序员欧阳子韩
 * 日期:2024/5/2 17:57
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public PageCommentsRes<CommentsBlogsUserPageDto> queryLikes(PageCommentsReq pageLikesReq) {
        ValidatorUtil.validateEntity(pageLikesReq);

        long pageNum = pageLikesReq.getPageNum();
        long pageSize = pageLikesReq.getPageSize();

        IPage<CommentsBlogsUserPageDto> page = new Page<>(pageNum, pageSize); // 这个 Page 对象可能不会被直接使用，只是用于传递参数
        QueryWrapper<BlogUserPageDto> wrapper = new QueryWrapper<>();

        // 调用自定义的 selectMapsPage 方法，并传递参数
        List<CommentsBlogsUserPageDto> commentsList = commentsMapper.selectMapsPage(page, wrapper, pageLikesReq.getBlogId());

        return PageCommentsRes
                .<CommentsBlogsUserPageDto>builder()
                .comments(commentsList)
                .pages(page.getPages())
                .pageNum(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteById(Integer id,HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("username", Objects.requireNonNull(TokenUtils.getCurrentUser()).getUsername());
        baseMapper.deleteById(id);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addComment(AddCommentReq addCommentReq, HttpServletRequest request) {
        ValidatorUtil.validateEntity(addCommentReq);

        Comments comments = new Comments();
        comments.setContent(addCommentReq.getContent());
        comments.setCreateUser(addCommentReq.getCreateUser());
        comments.setBlogId(addCommentReq.getBlogId());

        HttpSession session = request.getSession();
        session.setAttribute("username", Objects.requireNonNull(TokenUtils.getCurrentUser()).getUsername());

        baseMapper.insert(comments);
        return true;
    }

    @Override
    public PageCommentsRes<CommentsBlogsUserPageDto> queryUserLikes(PageUserCommentsReq pageUserCommentsReq) {
        ValidatorUtil.validateEntity(pageUserCommentsReq);

        long pageNum = pageUserCommentsReq.getPageNum();
        long pageSize = pageUserCommentsReq.getPageSize();

        IPage<CommentsBlogsUserPageDto> page = new Page<>(pageNum, pageSize); // 这个 Page 对象可能不会被直接使用，只是用于传递参数
        QueryWrapper<BlogUserPageDto> wrapper = new QueryWrapper<>();

        // 调用自定义的 selectMapsPage 方法，并传递参数
        List<CommentsBlogsUserPageDto> commentsList = commentsMapper.selectMapsUserPage(page, wrapper, pageUserCommentsReq.getCreateUser());

        return PageCommentsRes
                .<CommentsBlogsUserPageDto>builder()
                .comments(commentsList)
                .pages(page.getPages())
                .pageNum(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

}
